package org.beehoo.web.daily.signin.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.beehoo.web.daily.signin.service.SignInService;
import org.beehoo.web.daily.usermanage.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/signIn.beehoo")
public class signinController {
	@Autowired
	private SignInService signInService;
	public signinController(){
		
	}
	
	@RequestMapping(params = "method=list", method = RequestMethod.GET)
	//@ModelAttribute("users")
	@ResponseBody
	public Map<String, Object> getUserMaps(HttpServletRequest req) {
		List<UserVO> voList = signInService.getUserList();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("rows", voList);
		modelMap.put("records", voList.size());
		modelMap.put("page", "1");
		modelMap.put("total", "1");
		//modelMap.put("success", "true");
		return modelMap;
	}
	
	@RequestMapping
	public String load(ModelMap modelMap){
		return "signin";
	}
	
	@RequestMapping(params = "method=signIn")
	private String signIn(HttpServletRequest req, ModelMap modelMap) throws ServletException, IOException{
		String retUrl = "";
		req.setCharacterEncoding("utf-8");
		String userName = req.getParameter("emailSignup");
		boolean same = signInService.validateSameUserName(userName);
		if(same){
			//resp.sendRedirect(req.getContextPath() + "/signIn?sameUserName=true");
			modelMap.addAttribute("sameUserName", true);  
			retUrl = "redirect:signIn.beehoo";
		}
		UserVO vo = new UserVO();
		vo.setUserName(userName);
		vo.setPassword(req.getParameter("newPassword"));
		vo.setType("1");
		vo.setRegisterTime(new Date());
		vo.setStatus("1");
		req.getSession().setAttribute("user", signInService.signIn(vo));
		//resp.sendRedirect(req.getContextPath() + "/job");
		if(retUrl.equals("")){
			retUrl = "redirect:userMange.beehoo";
		}
		return retUrl;
	}
	
	@RequestMapping(params = "method=signInAjax", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getUser(HttpEntity<UserVO> model,HttpServletRequest req) {
		System.out.println("根据ID获取用户对象: " + model.getBody().getId());	
		model.getBody().setStatus("1");
		model.getBody().setRegisterTime(new Date());
		signInService.signIn(model.getBody());
		Map<String, Object> map = getUserMaps(req);
		List<UserVO> list = (List<UserVO>)map.get("data");
		list.add(model.getBody());
		map.put("total", list.size());
		map.put("data", list);
		return map;
	}
	
	@RequestMapping(params = "method=login")
	private String login(HttpServletRequest req, ModelMap modelMap) throws ServletException, IOException{
		String retUrl = "";
		try {
			req.setCharacterEncoding("utf-8");
			String userName = req.getParameter("emailSignin");
			String password = req.getParameter("signinPassword");
			UserVO user = signInService.getUser(userName);
			if(user == null){
				//resp.sendRedirect(req.getContextPath() + "/signIn?notExist=true");
				modelMap.addAttribute("notExist", true);  
				retUrl = "redirect:signIn.beehoo";
			}
			if(!user.getPassword().equals(password)){
				//resp.sendRedirect(req.getContextPath() + "/signIn?passwordErr=true");
				modelMap.addAttribute("passwordErr", true);  
				retUrl = "redirect:signIn.beehoo";
			}
			if(retUrl.equals("")){
				req.getSession().setAttribute("user", user);
				retUrl = "redirect:userMange.beehoo";
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			retUrl = "redirect:signIn.beehoo";
			//e.printStackTrace();
		}
		return retUrl;
		//resp.sendRedirect(req.getContextPath() + "/job");
	}
	
	@RequestMapping(params = "method=logout")
	private String logout(HttpServletRequest req, ModelMap modelMap) throws ServletException, IOException{
		req.setCharacterEncoding("utf-8");
		req.getSession().removeAttribute("user");
		//resp.sendRedirect(req.getContextPath() + "/job");
		return "redirect:userMange.beehoo";
	}

}
